package com.boomcat.boomcat.controller;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.boomcat.boomcat.config.WebSocketServer;
import com.boomcat.boomcat.domain.Expert;
import com.boomcat.boomcat.result.ExceptionCode;
import com.boomcat.boomcat.result.Result;
import com.boomcat.boomcat.service.ExpertService;
import com.boomcat.boomcat.util.JwtUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/expert")
@CrossOrigin(origins = "*")
public class ExpertController {
    @Autowired
    ExpertService expertService;
    @PostMapping("/register")
    Result register(@RequestBody JSONObject jsonObject) {
        System.out.println("register");

        String mailAddress = (String) jsonObject.get("mailAddress");
        String password = (String) jsonObject.get("password");
        String code = (String) jsonObject.get("code");
        String nickname = (String) jsonObject.get("nickname");


        //check password length and legal
        if(password.length()< 8){
            return Result.error(ExceptionCode.TOO_SHORT_PASSWORD);
        }
        else if(password.length() > 20){
            return Result.error(ExceptionCode.TOO_LONG_PASSWORD);
        }
        //密码不能含有中文
        boolean hasIllegalChar = !password.matches("^[a-zA-Z0-9]*$");
        if (hasIllegalChar) {
            return Result.error(ExceptionCode.ILLEGAL_PASSWORD);
        }

        //check nickname length and legal
        boolean nicknameIsPureChars=nickname.matches("^[a-zA-Z0-9]*$");
        if (nicknameIsPureChars) {
            //English can be 4-10
            if(nickname.length() > 10){
                return Result.error(ExceptionCode.TOO_LONG_NICKNAME);
            }
            else if (nickname.length() < 4) {
                return Result.error(ExceptionCode.TOO_SHORT_NICKNAME);
            }
        }
        else{
            //Chinese can be 1-7
            if(nickname.length() > 7){
                return Result.error(ExceptionCode.TOO_LONG_NICKNAME);
            }
            else if (nickname.length() < 1) {
                return Result.error(ExceptionCode.TOO_SHORT_NICKNAME);
            }
        }
        boolean hasSpecialChar = !nickname.matches("^[a-zA-Z0-9\u4e00-\u9fa5]*$");
        if (hasSpecialChar){
            return Result.error(ExceptionCode.ILLEGAL_NICKNAME);
        }

        Expert expert=new Expert();
        expert.setName(nickname);
        expert.setAccount(mailAddress);
        expert.setPassword(password);
        expertService.register(expert);
        return Result.success();
    }

    @PostMapping("/login")
    Result login(@RequestBody JSONObject jsonObject) {
        System.out.println("login");

        String account = (String) jsonObject.get("account");
        String password = (String) jsonObject.get("password");
        System.out.println("account="+account+" password="+password);

//        select expert in db
        Expert expert = expertService.selectExpert (account, password);
        if (expert != null) {
            System.out.println("登陆成功");
            JSONObject resp=new JSONObject();

            String token = JwtUtil.createJwt(expert.getId().toString());
            System.out.println("token="+token);
            resp.put("token",token);
            resp.put("name",(expert.getName()));
            resp.put("avatar",expert.getAvatar());
            return Result.success(resp);
        }
        return Result.error(ExceptionCode.WRONG_PASSWORD);
    }

    @GetMapping("/showOnline")
    Result showOnline() {
        System.out.println("show online");
        List<Long> onlineIds = WebSocketServer.getOnlineIds();
        System.out.println("在线用户id");
        onlineIds.forEach(System.out::println);

        System.out.println("现有用户");
        List<Expert> experts = expertService.list();
        experts.forEach(System.out::println);

        JSONArray jsonArray = new JSONArray();

        for(Expert tmp:experts){
            if (onlineIds.contains(tmp.getId())){
                tmp.setOnline(1);
                jsonArray.add(tmp);
            }
        }
        return Result.success(jsonArray);


    }

    @GetMapping("/showAll")
    Result showAll() {
        System.out.println("show all");
        List<Expert> experts = expertService.list();
        JSONArray jsonArray = new JSONArray();
        List<Long> onlineIds = WebSocketServer.getOnlineIds();
        experts.forEach(tmp->{
            if  (onlineIds.contains(tmp.getId())){
                tmp.setOnline(1);
            }
        });

        jsonArray.addAll(experts);
        return Result.success(jsonArray);
    }




}
